<?php

namespace backend\controllers;

use Yii;
use common\models\SysConfig;
use yii\helpers\Url;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;

/**
 * 网站基础设置
 * Class SysconfigController
 * @package backend\controllers
 */
class SysconfigController extends DashboardController
{
    # 编辑 | 添加页面
    public function actionSeo()
    {
       $seo = SysConfig::getFindOne();
        return $this->render('seo',['seo' => $seo]);
    }

    # 执行更新
    public function actionUpdate()
    {
        if (Yii::$app->request->isPost){
            $post = Yii::$app->request->post();
            $model = isset($post['id']) && !empty($post['id']) ? SysConfig::find()->where(['id' => $post['id']])->one() : new SysConfig();

            $model->desc = $post['desc'];
            $model->title = $post['title'];
            $model->keywords = $post['keywords'];
            $model->copyright = $post['copyright'];
            $model->recnumber = $post['recnumber'];
            $model->motto = $post['motto'];

            if ($model->validate() && $model->save()){
                $this->addFlash('success',"恭喜小主，修改成功！");
                return $this->redirect('seo');
            }else{
                $this->addFlash('error','验证失败');
            }

        }else{
            throw new HttpException('The visited is illegal.');
        }
    }
}